//package com.xw.util;
//
//
//
//import com.amazonaws.AmazonClientException;
//import com.amazonaws.AmazonServiceException;
//import com.amazonaws.auth.PropertiesCredentials;
//import com.amazonaws.services.s3.AmazonS3;
//import com.amazonaws.services.s3.AmazonS3Client;
//import com.amazonaws.services.s3.model.Bucket;
//import com.amazonaws.services.s3.model.GetObjectRequest;
//import com.amazonaws.services.s3.model.PutObjectRequest;
//import com.amazonaws.services.s3.model.S3Object;
//
//import  java.io.BufferedReader;
//
//import  java.io.File;
//
//import  java.io.FileOutputStream;
//
//import  java.io.IOException;
//
//import  java.io.InputStream;
//
//import  java.io.InputStreamReader;
//
//import  java.io.OutputStreamWriter;
//
//import  java.io.Writer;
//
//
//public class OOSSample {
//
//    public static void main(String[] args) throws IOException {
//        /* 创建client,其中OOSCredentials.properties中存放着用户名和密码*/
//        AmazonS3 oos = new AmazonS3Client(new PropertiesCredentials(
//                OOSSample.class.getClassLoader()
//                        .getResourceAsStream("OOSCredentials.properties")));
//
//        // 设置endpoint
//        oos.setEndpoint("http://oos-cn.ctyunapi.cn");
//
//        String bucketName = "video1";
//        //文件名
//        String key = "ccc";
//
//        System.out.println("=====================================");
//        System.out.println("Getting Started with OOS");
//        System.out.println("=====================================");
//
//        try {
//
//            /* 创建bucket */
//
//            System.out.println("Creating bucket " + bucketName + "\n");
//
////            oos.createBucket(bucketName);
//
//
//
//            /*列出账户内的所有buckets */
//
//            System.out.println("Listing buckets");
//
//            for (Bucket bucket : oos.listBuckets()) {
//
//                System.out.println(" - " + bucket.getName());
//
//            }
//
//            System.out.println();
//
//
//
//            /* 上传一个object到bucket中 */
//
//            System.out.println("上传一个object到bucket中");
//
//            oos.putObject(new PutObjectRequest(bucketName, key,
//
//                    createSampleFile()));
//
//
//
//            /* 下载object */
//
//            System.out.println("下载object");
//
//            /* 当使用getObject()方法时，需要非常小心。因为返回的S3Object对象包括一个从
//
//            HTTP连接获得的数据流。底层的HTTP连接不会被关闭，直到用户读完了数据，并关闭了流。因此从S3Object中读取inputStream数据后，需要立刻关闭流。否则会导致客户端连接池用满 */
//
//            S3Object object = oos.getObject(new GetObjectRequest(bucketName, key));
//
//            System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
//
//            System.out.println("Content:");
//
//            displayTextInputStream(object.getObjectContent());
//
//
//
//            /* 拷贝object */
//
////            String destinationBucketName = "my-copy-oos-bucket";
////
////            String destinationKey = "MyCopyKey";
////
////            System.out.println("Copying an object ,from " + bucketName + "/"
////
////                    + key + " to " + destinationBucketName + "/"
////
////                    + destinationKey);
////
////            oos.createBucket(destinationBucketName);
////
////            oos.copyObject(bucketName, key, destinationBucketName,
////
////                    destinationKey);
//
//
//
//            /* 下载拷贝的object */
//
////            System.out.println("Downloading the " + destinationKey + " object");
////
////            object = oos.getObject(new GetObjectRequest(destinationBucketName,
////
////                    destinationKey));
////
////            System.out.println("Content-Type: "
////
////                    + object.getObjectMetadata().getContentType());
////
////            System.out.println("Content:");
////
////            displayTextInputStream(object.getObjectContent());
//
//
//
//            /* 列出bucket中的object，支持prefix,delimiter,marker,max-keys等选项 */
//
////            System.out.println("Listing objects");
////
////            ObjectListing objectListing = oos
////
////                    .listObjects(new ListObjectsRequest().withBucketName(
////
////                            bucketName).withPrefix("My"));
////
////            for (S3ObjectSummary objectSummary : objectListing
////
////                    .getObjectSummaries()) {
////
////                System.out.println(" - " + objectSummary.getKey() + "  "
////
////                        + "(size = " + objectSummary.getSize() + ")");
////
////            }
////
////            System.out.println();
//
//
//
//            /* 删除object */
//
////            System.out.println("Deleting objects\n");
////
////            oos.deleteObject(bucketName, key);
////
////            oos.deleteObject(destinationBucketName, destinationKey);
//
//
//
//            /* 删除bucket */
//
////            System.out.println("Deleting bucket " + bucketName + "\n");
////
////            oos.deleteBucket(bucketName);
////
////            System.out.println("Deleting bucket " + destinationBucketName
////
////                    + "\n");
////
////            oos.deleteBucket(destinationBucketName);
//
//        } catch (AmazonServiceException ase) {
//
//            System.out.println("Caught an AmazonServiceException, which means your request made it "
//
//                    + "to OOS, but was rejected with an error response for some reason.");
//
//            System.out.println("Error Message:    " + ase.getMessage());
//
//            System.out.println("HTTP Status Code: " + ase.getStatusCode());
//
//            System.out.println("OOS Error Code:   " + ase.getErrorCode());
//
//            System.out.println("Request ID:       " + ase.getRequestId());
//
//        } catch (AmazonClientException ace) {
//
//            System.out.println("Caught an AmazonClientException, which means the client encountered "
//
//                    + "a serious internal problem while trying to communicate with OOS, "
//
//                    + "such as not being able to access the network.");
//
//            System.out.println("Error Message: " + ace.getMessage());
//
//        }
//
//    }
//
//
//    private static File createSampleFile() throws IOException {
//
//        File file = File.createTempFile("oos-java-sdk-", ".txt");
//
//        file.deleteOnExit();
//
//        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
//
//        writer.write("abcdefghijklmnopqrstuvwxyz\n");
//
//        writer.write("01234567890112345678901234\n");
//
//        writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
//
//        writer.write("01234567890112345678901234\n");
//
//        writer.write("abcdefghijklmnopqrstuvwxyz\n");
//
//        writer.close();
//
//        return file;
//
//    }
//
//
//
//    private static void displayTextInputStream(InputStream input)throws IOException {
//
//        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
//
//        while (true) {
//
//            String line = reader.readLine();
//
//            if (line == null)
//
//                break;
//
//            System.out.println("    " + line);
//
//        }
//
//        /* 需要在这里关闭InputStream，原因参见getObject处 */
//
//        input.close();
//
//        System.out.println();
//
//    }
//}
//
//
//
//
